//
//  UISearchBar+Extension.swift
//  SwiftProject
//
//  Created by qindong on 2021/5/20.
//

import Foundation

extension UISearchBar {
    private func textFieldBackgroundImage(_ height : CGFloat, _ backgroundColor : UIColor, _ cornerRadius : CGFloat) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: qmui_width, height: height)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        
        let bez = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
        bez.lineWidth = 0.5
        backgroundColor.set()
        bez.fill()
        bez.stroke()
        
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
    
    // MARK: - 设置searchbar背景色 解决searchbar放在导航上不适配问题
    func setSearchBarFieldHeight(_ height : CGFloat, _ color : UIColor = .white, _ cornerRadius : CGFloat = 0) {
        setSearchFieldBackgroundImage(textFieldBackgroundImage(height, color, cornerRadius), for: .normal)
    }
}
